package cn.lanqiao.facelogin.faceData;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.facebody20191230.models.DetectLivingFaceRequest;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LiveFaceDetection {
    public static Boolean faceQuery(String string) throws Exception {
        com.aliyun.facebody20191230.Client client = LiveFaceDetection.createClient("LTAI5tHNDzFHTvU8ZuETPGY9", "FxOXeVcFkA8Q7UuHFnTxGQDnSGqkhb");
        DetectLivingFaceRequest.DetectLivingFaceRequestTasks tasks0 = new DetectLivingFaceRequest.DetectLivingFaceRequestTasks()
//                .setImageURL("https://demo55.oss-cn-shanghai.aliyuncs.com/shz4.jpg?Expires=1639786331&OSSAccessKeyId=TMP.3KeWQ7jq1WWyCMvrNNgDQwWY5gvruYHc2rNrLe8dvnjCXp5q5cHsqEm3eNSMoCrpb4mHaAEnZgwa9YFXYTMec1s1gwXQUf&Signature=6RZjD3O2Kk2snsEd4EJJXMyQ3cc%3D");
                .setImageURL(string);
        DetectLivingFaceRequest detectLivingFaceRequest = new DetectLivingFaceRequest()
                .setTasks(java.util.Arrays.asList(
                        tasks0
                ));
        // 复制代码运行请自行打印 API 的返回值
//        com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(client.detectLivingFace(detectLivingFaceRequest)));
        List<Map<String, Object>> mapList = new ArrayList<>();
        String s = Common.toJSONString(client.detectLivingFace(detectLivingFaceRequest));
        JSONObject json = JSONObject.parseObject(s);
        JSONObject body = json.getJSONObject("body");
        JSONObject data = body.getJSONObject("data");
        JSONArray elements = data.getJSONArray("elements");
        for (int i = 0; i < elements.size(); i++) {
            JSONObject results = elements.getJSONObject(i);
            String results1 = results.getString("results");
            JSONArray resultsArray = JSONObject.parseArray(results1);
            for (int j = 0; j < resultsArray.size(); j++) {
                JSONObject o = resultsArray.getJSONObject(j);
                HashMap<String, Object> map = new HashMap<>();
                map.put("rate", o.get("rate"));
                map.put("suggestion", o.get("suggestion"));
                map.put("label", o.get("label"));
                mapList.add(map);
            }
        }
        for (int i = 0; i < mapList.size(); i++) {
            System.out.println(mapList);
            Map<String, Object> map = mapList.get(i);
            Object rate = map.get("rate");
            BigDecimal rateTwo = new BigDecimal(rate + "");
            BigDecimal rateThree = new BigDecimal(36);
            Object suggestion = map.get("suggestion");
            Object label = map.get("label");
            if ("pass".equals(suggestion) && "normal".equals(label)) {
                if (rateTwo.compareTo(rateThree) > 0) {
                    System.out.println("人脸活体检测为正确");
                    return true;
                }
                System.out.println("人脸活体检测为错误");
                return false;
            }

        }
        return false;
    }

    public static com.aliyun.facebody20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "facebody.cn-shanghai.aliyuncs.com";
        return new com.aliyun.facebody20191230.Client(config);
    }
}
